363D - Renting Bikes - CodeForces Solution


binary search greedy *1800

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os


def valid(i, j, n, m):
    if i < n and i >= 0 and j >= 0 and j < m:
        return True      return False


def sumn(i, n):
    return (n-i)*(i+n)/2


def sqfun(a, b, c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a


def getprime(num):
    if all(num % i != 0 for i in range(2, int(math.sqrt(num))+1)):
        return True



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]


def get(mid):
    ind=0
    need=0
    for i in range(n-mid,n):
       
        if boymoney[i]<bikeprice[ind]:
            need+=bikeprice[ind]-boymoney[i]
        ind+=1
    return need<=money


def solve():
    global n,m,boymoney,bikeprice,money
    n, m, money = values()
    boymoney = sorted(values())
    bikeprice = sorted(values())
    l=0;r=min(n, m)
    while l<=r:
        mid=l +(r-l)//2
        if get(mid):l=mid+1
        else:r=mid-1
    print(r,max(0,sum(bikeprice[:r])-money))
   
if __name__ == "__main__":
        solve()

C++ Code:

#include<bits/stdc++.h>

#define int int64_t

using namespace std;

int mn_a = 1e9;
bool f(vector<int> &b, vector<int>&p, int a, int x) {

    if ( (x > b.size()) || (x > p.size()))
        return false;

    int btb = x-1;

    for (int i = 0; i < x; i++) {

        if (b[i] >= p[btb]) {
            btb--;
            continue;
        }

        if (b[i] < p[btb]) {
            if (a - (p[btb]-b[i]) >= 0) {
                a -= (p[btb] - b[i]);
                btb--;
                continue;
            } else return false;
            // falla la plata compartida
        }
        
    }

    return true;
}

signed main() {

    int n, m, a;
    cin >> n >> m >> a;

    vector<int> b(n), p(m);
    for (int i = 0; i < n; i++)
        cin >> b[i];

    for (int i = 0; i < m; i++)
        cin >> p[i];


    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    sort(p.begin(), p.end());

    int l = 0, r = n;
    while (l != r) {
        int mid = (l+r+1)/2;
        if (!f(b, p, a, mid))
            r = mid - 1;
        else l = mid;
    }

    int sum = 0;
    for (int i = 0; i < l; i++)
        sum += p[i];

    cout << l << " " << max((sum - a), (int)0) << endl;


    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST